import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 5
 * Date: 17-2-20
 * Time: 下午1:28
 * To change this template use File | Settings | File Templates.
 */
public class Visiable {
    static volatile int count = 0;

    @Test
    public void test() {

        List list=new ArrayList(500);
        list.clear();

        new Thread(new Write(count)).start();
        for (int i = 0; i < 5; i++) {
            new Thread(new Read(count)).start();
        }
    }

}

class Write implements Runnable {
    private int count;

    Write(int count) {
        this.count = count;
    }

    @Override
    public void run() {
        while (count < 100) {
            count++;
            System.out.println(Thread.currentThread().getName() + " : " + count);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }
}

class Read implements Runnable {
    private int count;

    Read(int count) {
        this.count = count;
    }

    @Override
    public void run() {
        while (true)synchronized (this){
            System.out.println(Thread.currentThread().getName() + " : " + count);
        }
    }
}
